<template>
  <div class="history-sessions-container">
    <div class="header">
      <h2>历史会话</h2>
      <n-button @click="refreshSessions" :loading="loading" type="primary" size="small">
        <template #icon>
          <n-icon><RefreshOutline /></n-icon>
        </template>
        刷新
      </n-button>
    </div>
    
    <div class="sessions-list" v-if="!loading && sessions.length > 0">
      <div 
        v-for="session in sessions" 
        :key="session.sessionId || session.id"
        class="session-item"
        @click="openSession(session)"
      >
        <div class="session-title">{{ session.title || '未命名会话' }}</div>
        <div class="session-info">
          <span class="session-time">{{ formatTime(session.createTime || session.lastActiveAt) }}</span>
          <span class="message-count">{{ session.messageCount || 0 }} 条消息</span>
        </div>
      </div>
    </div>
    
    <div v-else-if="!loading && sessions.length === 0" class="empty-state">
      <n-empty description="暂无历史会话" />
    </div>
    
    <div v-if="loading" class="loading-state">
      <n-spin size="large" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { NButton, NIcon, NEmpty, NSpin } from 'naive-ui'
import { RefreshOutline } from '@vicons/ionicons5'
import { aiService } from '../../shared/services/ai-service'

const router = useRouter()
const loading = ref(false)
const sessions = ref<any[]>([])

// 加载会话列表
const loadSessions = async () => {
  loading.value = true
  try {
    const response = await aiService.getSessionHistoryList()
    if (response && response.code === 0 && Array.isArray(response.data)) {
      sessions.value = response.data
    }
  } catch (error) {
    console.error('加载会话列表失败:', error);
  } finally {
    loading.value = false
  }
}

// 刷新会话列表
const refreshSessions = () => {
  loadSessions()
}

// 打开会话
const openSession = (session: any) => {
  const sessionId = session.sessionId || session.id
  router.push(`/chat/${sessionId}`)
}

// 格式化时间
const formatTime = (timestamp: string | number) => {
  const date = new Date(timestamp)
  return date.toLocaleString('zh-CN', { 
    year: 'numeric', 
    month: '2-digit', 
    day: '2-digit', 
    hour: '2-digit', 
    minute: '2-digit' 
  })
}

onMounted(() => {
  loadSessions()
})
</script>

<style scoped>
.history-sessions-container {
  padding: 20px;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header h2 {
  margin: 0;
  font-size: 1.5rem;
}

.sessions-list {
  flex: 1;
  overflow-y: auto;
}

.session-item {
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: all 0.3s;
  border: 1px solid #e0e0e0;
  background-color: #fff;
}

.session-item:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border-color: #1890ff;
}

.session-title {
  font-weight: 500;
  margin-bottom: 5px;
  font-size: 1rem;
}

.session-info {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  color: #666;
}

.empty-state, .loading-state {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
}
</style>